package com.dailyblue.javaee.dao;

import com.dailyblue.javaee.bean.User;
import com.dailyblue.javaee.db.DBManager;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAO {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    @SneakyThrows
    public User login(String name,String password){
        String sql = "select * from `user` where name=? and password=?";//加密用Md5();
        System.out.println(sql);
        System.out.println(name);
        System.out.println(password);
        conn = DBManager.getConnection();
        ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ps.setString(2,password);
        rs = ps.executeQuery();
        if(rs.next()){
            User user = new User();
            user.setId(rs.getInt(1));
            user.setName(name);
            user.setNickname(rs.getString(4));
            return user;
        }
        return null;
    }
}